//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _APPS_BUSH_PARSER_CWORD_H_
#define _APPS_BUSH_PARSER_CWORD_H_

#include <buola/buola.h>
#include <buola/io.h>

#include "ctoken.h"

namespace buola { namespace bush {

class CWord
{
public:
    enum EFormat
    {
        FORMAT_NONE=0,
        FORMAT_ESCAPED=(1<<0), //some character is escaped with backslash
        FORMAT_BRACES=(1<<1),
        FORMAT_GLOB=(1<<2),
        FORMAT_TILDE=(1<<4),
    };
    DECLARE_FRIEND_ENUM_BITOPS(EFormat);

    CWord()
        :   mFormat(FORMAT_NONE)
    {}
    
    CWord(const CWord &pRH)
        :   mChars(pRH.mChars)
        ,   mFormat(pRH.mFormat)
    {}
    
    CWord(CWord &&pRH)
        :   mChars(std::move(pRH.mChars))
        ,   mFormat(pRH.mFormat)
    {}

    CWord(CWord &p1,CWord &p2)
        :   mChars(p1.mChars+p2.mChars)
        ,   mFormat(p1.mFormat|p2.mFormat)
    {}
    
    CWord &operator=(const CWord &pRH)
    {
        mChars=pRH.mChars;
        mFormat=pRH.mFormat;
        return *this;
    }

    void AppendTokens(const TTokenRange &pR);
    void AppendRawText(const std::string &pText);
    void AppendQuotedText(const std::string &pText);

    static std::wstring RawToEscaped(const std::wstring::const_iterator &pB,
                                    const std::wstring::const_iterator &pE);
    static std::wstring QuotedToEscaped(const std::wstring::const_iterator &pB,
                                    const std::wstring::const_iterator &pE);
    static std::string RawToEscaped(const std::string::const_iterator &pB,
                                   const std::string::const_iterator &pE);
    static std::string QuotedToEscaped(const std::string::const_iterator &pB,
                                   const std::string::const_iterator &pE);
    static std::wstring EscapeBack(const std::wstring::const_iterator &pB,
                                    const std::wstring::const_iterator &pE);
    static std::wstring Quote(const std::wstring::const_iterator &pB,
                                    const std::wstring::const_iterator &pE);
    
    void ExpandTilde();
    void GetUnescaped(std::string &pDst);
    
    void Print(int pDepth=0)
    {
        for(int i=0;i<pDepth;++i)
            msg_info() << "   ";
        msg_info() << '\"' << mChars << "\"\n";
    }

    std::string mChars;
    EFormat mFormat;
};
    
/*namespace bush*/ } /*namespace buola*/ }

#endif